import { reactive } from "vue";
import type { UserDeptJobInfoRes } from "./userLog/types";
import { userLogUser } from "./userLog/index";

export class RbacApi {
  // 用户信息缓存
  userInfoMap: Map<string, UserDeptJobInfoRes> = new Map();
  // 默认用户信息
  defauleUserInfo = reactive({} as UserDeptJobInfoRes);
  // 获取用户信息
  userInfo(userId: string, tenant?: string): UserDeptJobInfoRes {
    if (userId == undefined || userId === "") {
      return this.defauleUserInfo;
    }
    if (!this.userInfoMap.has(userId)) {
      const user = reactive({} as UserDeptJobInfoRes);
      this.userInfoMap.set(userId, user);
      userLogUser(userId, tenant).then((res) => {
        Object.assign(user, res.data);
      });
    }
    return this.userInfoMap.get(userId) as UserDeptJobInfoRes;
  }
}

const rbacApi = new RbacApi();

export { rbacApi };
